package net.wsfcp.android.http.mvp.presenter;

import net.wsfcp.android.base.BasePresenter;
import net.wsfcp.android.bean.resultbean.UserBean;
import net.wsfcp.android.http.mvp.IModel;
import net.wsfcp.android.http.mvp.contract.LoginContract;
import net.wsfcp.android.http.mvp.model.LoginModel;
import net.wsfcp.android.ui.view.my.fragment.LoginFragment;
import net.wsfcp.android.utils.LogUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
 * Created by Shaoboyang on 2018/12/14.
 */

//登录
public class LoginPresenter extends BasePresenter<LoginFragment> implements
        LoginContract.LoginPresenter {

    @Override
    public void login(final String name, final String pwd, String code) {
        if (!getIView().checkNull()) {
            ((LoginModel) getiModelMap().get("login")).login(name, pwd,code, new LoginModel
                    .LoginInfoHint() {
                @Override
                public void loginSuccessInfo(UserBean loginBean) {
                    getIView().loginSuccess(loginBean,name,pwd);  //成功
                }

                @Override
                public void loginFailInfo(String str) {
                    LogUtils.e("LoginPresenter.loginfailInfo", str);

                    getIView().loginFail(str);  //失败
                }



            });
        }
    }

    @Override
    public void getImageCode() {
        ((LoginModel) getiModelMap().get("imagecode")).getCode(new LoginModel
                .ImageInfoHint() {

            @Override
            public void getImageCodeSuccessInfo(InputStream inputStream) {

                try {
                    getIView().getImageCodeSuccess(inputStream);  //成功
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void getImageCodeFailInfo(String str) {
                getIView().getImageCodeFail(str);  //失败
            }

        });
    }




    @Override
    public HashMap<String, IModel> getiModelMap() {
        return loadModelMap(new LoginModel());
    }

    @Override
    public HashMap<String, IModel> loadModelMap(IModel... models) {
        HashMap<String, IModel> map = new HashMap<>();
        map.put("login", models[0]);
        map.put("imagecode", models[0]);
        return map;
    }
}
